// pages/coupon/coupon.js
const api = require("../../utils/interface.js")
const math = require("../../utils/math.js")
const tools = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponList: [],
    routerData: null,
    superposition: [],
    currentIndex: 0,
    imagestate: 0,//判断是否可叠加的状态
    selectCoupon: [],
    orderAmount: 0,
    tabIndex:0//上方切换状态
  },
  onLoad: function (options) {
    this.getCouponList(0, "", options.membercardId)
    if(options.membercardId){
      wx.setNavigationBarTitle({
        title: "红包"
      })
    }else{
      wx.setNavigationBarTitle({
        title: "优惠卡券"
      })
    }
    console.log(options,"11")
    this.setData({
      membercardId:options.membercardId
    })
  },
  goback() {
    wx.navigateBack({
      delta: 1
    })
  },
  godetails(e) {
    console.log(e, "e")
    let couponId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/coupondetails/details?memberCouponId=' + couponId
    })
  },
  handleClick(){
    // this.getCouponList()
  },
  //选项卡
  select(e) {
    let index = e.currentTarget.dataset.index;
    if(index==0){
      this.getCouponList(0, "", this.data.membercardId)
    }
    if(index==1){
      this.getCouponList(1, "", this.data.membercardId)
    }
    if(index==2){
      this.getCouponList("", 0, this.data.membercardId)
    }
    this.setData({
      tabIndex: index
    })
  },
  // 获取优惠券列表
  getCouponList(used, timeOutStatus,membercardId) {
    wx.showToast({ title: '加载中', icon: 'loading' });
    let user = wx.getStorageSync('userInfo').id
    console.log(user,"11")
    api.getMemberCouponList({ memberId: user, used: used, timeOutStatus: timeOutStatus, memberCardId:membercardId}).then(res => {
      console.log(res,"res")
      if (res.data.result.length > 0) {
        res.data.result.map((item,index)=>{
          item.preAmount = math.divide(item.preAmount, 100)
          item.minimumAmount = math.divide(item.minimumAmount, 100)
        })
        this.setData({
          couponList: res.data.result
        })
        wx.hideToast();
      }else{
        this.setData({
          couponList:[]
        })
        wx.hideToast();
      }

    })
  },
  


})